package com.qk.management.controller;

import com.qk.common.Result;
import com.qk.common.anno.Logs;
import com.qk.entity.Dept;
import com.qk.management.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 *
 *
 * @author blusr
 * @version 1.0
 * @date 2025/9/29 10:31
 */
@Slf4j
@RestController
@RequestMapping("/depts")
@SuppressWarnings("all")
public class DeptController {
    @Autowired
    private DeptService deptService;

    @GetMapping
    public Result page(
            @RequestParam(name = "name", required = false) String name,
            @RequestParam(name = "status", required = false) Integer status,
            @RequestParam(name = "page", defaultValue = "1") Integer page,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        log.info("条件分页查询部门: name={}, status={}, page={}, pageSize={}", name, status, page, pageSize);
        return Result.success(deptService.page(name, status, page, pageSize));
    }

    @Logs("添加部门")
    @PostMapping
    public Result addDept(@RequestBody Dept dept){
        log.info("添加部门: {}", dept);
        deptService.addDept(dept);
        return Result.success();
    }

    @Logs("根据ID查询部门详情")
    @GetMapping("/{id}")
    public Result getDeptById(@PathVariable("id") Integer id){
        log.info("根据ID查询部门: id={}", id);
        return Result.success(deptService.getDeptById(id));
    }

    @Logs("更新部门")
    @PutMapping
    public Result updateDept(@RequestBody Dept dept){
        log.info("更新部门: {}", dept);
        deptService.updateDept(dept);
        return Result.success();
    }

    @Logs("根据ID删除部门")
    @DeleteMapping("/{id}")
    public Result deleteDept(@PathVariable("id") Integer id){
        log.info("根据ID删除部门: id={}", id);
        deptService.deleteDept(id);
        return Result.success();
    }


    @GetMapping("/list")
    public Result listDept(){
        log.info("查询所有部门");
        return Result.success(deptService.listDept());
    }
}
